// Pages/xuqiujiaoyi/xuqiujiaoyi.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '2019-01-01',
    index: 0,/**+1 */
    selectorc: ["金钱交易", "物物交易"],
    exchangeThingId: 0,
    wexin: '',
    price: '',
    thisngsId: ''/**页面跳转时赋值 */,
    thid:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    console.log(options.thingsId + "qweqwe")
    that.setData({
      thisngsId: options.thingsId/**id */
    })
  },

  inwexin: function (e) {
    /*微信*/
    this.setData({
      wexin: e.detail.value
    })
  },
  inprice: function (e) {
    /**价格 */
    this.setData({
      price: e.detail.value
    })
  }
  ,
  inthingid: function (e) {
    /*物物id*/
    this.setData({
      exchangeThingId: e.detail.value
    })
  }
  ,
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindDateChange: function (e) {/**时间 */
    console.log(e)
    this.setData({
      date: e.detail.value
    })
  },
  changbusiness: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  thidset:function(e){
    console.log(e)
    this.setData({
      thid:e.detail.value
    })
  },
  sendForm: function () {
    var token = wx.getStorageSync('token')
    var that=this
    console.log(this.data.date + "" + this.data.exchangeThingId + "" + this.data.wexin + "" + this.data.price + "" + this.data.thingsId)
    console.log(this.data.index + 1)
   if(that.data.exchangeThingId==0){
     wx.request({
       method: "POST",
       url: 'http://47.103.21.65:5645/require/finishRequirement',

       data: {  //参数传递

         finishedDate: this.data.date,/**时间 */
         finished: this.data.index + 1,
         // thingToThingId: this.data.exchangeThingId,
         exchangeProductId: parseInt(this.data.thid),
         weixin: this.data.wexin,
         finishPrice: this.data.price,
         requirementId: this.data.thisngsId/**需求id */
       },
       header: { //设置请求头
         'content-type': 'application/x-www-form-urlencoded',// 默认值//header:{...}用啥设置啥，
         'token': token
       },

       success: function (res) {
         console.log(res);
         if (res.data.code == 1) {
           wx.showToast({
             title: '交易成功',
             icon: 'success'
           })
           setTimeout(function () {
             wx.navigateBack({
               delta: 2
             })
           }, 1000)

         } else {
           wx.showToast({
             title: '交易失败',
             icon: 'fail'
           })
         }
       },
       fail: function (error) { //请求失败
         console.log(error);

       }
     })
   }else{
    wx.request({
      method: "POST",
      url: 'http://47.103.21.65:5645/require/finishRequirement',

      data: {  //参数传递

        finishedDate: this.data.date,/**时间 */
        finished: this.data.index + 1,
        thingToThingId: this.data.exchangeThingId,
        exchangeProductId: parseInt(this.data.thid) ,
        weixin: this.data.wexin,
        finishPrice: this.data.price,
        requirementId: this.data.thisngsId/**需求id */
      },
      header: { //设置请求头
        'content-type': 'application/x-www-form-urlencoded',// 默认值//header:{...}用啥设置啥，
        'token': token
      },

      success: function (res) {
        console.log(res);
        if (res.data.code==1){
        wx.showToast({
          title: '交易成功',
          icon: 'success'
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 2
          })
        }, 1000)
      
        }else{
          wx.showToast({
            title: '交易失败',
            icon: 'fail'
          })
        }
      },
      fail: function (error) { //请求失败
        console.log(error);

      }
    })
    }
  }

})